package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.factories.imp;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.command.RegisterCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.devicecontroller.factories.DeviceManagerFactory;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.CompoundCommand;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Device;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.CompoundCommandService;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.factories.CompoundCommandServiceFactory;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.imp.CompoundCommandServiceImp;

@Component
public class CompoundCommandServiceFactoryImp implements
		CompoundCommandServiceFactory {

	@Autowired(required = true)
	private DeviceManagerFactory devman;
	
	@Override
	public List<CompoundCommandService> create(
			List<CompoundCommand> commands, Device device) {
		List<CompoundCommandService> ret = new ArrayList<CompoundCommandService>();
		for (CompoundCommand cmpcmd : commands) {
			ret.add(create(cmpcmd,device));
		}
		return ret;
	}
	
	@Override
	public CompoundCommandService create(CompoundCommand command,Device device) {
		CompoundCommandServiceImp serv = new CompoundCommandServiceImp();
		serv.setDevman(devman.create(device));
		serv.setCmpcmd(command);
		RegisterCommand cmd = new RegisterCommand();
		cmd.setAddress(command.getAddress());
		serv.setCmd(cmd);
		return serv;
	}

	/**
	 * @return the devman
	 */
	public DeviceManagerFactory getDevman() {
		return devman;
	}

	/**
	 * @param devman the devman to set
	 */
	public void setDevman(DeviceManagerFactory devman) {
		this.devman = devman;
	}


}
